import asyncio
import time
import ygg_aiomysql as aiomysql

async def main():
    """
    示例11: 获取数据库中所有表名
    """
    # 使用时间戳生成唯一数据库名和表名
    test_db_name = f"test_db_{int(time.time())}"
    test_table1 = f"test_table1_{int(time.time())}"
    test_table2 = f"test_table2_{int(time.time())}"
    
    # 创建临时连接池
    pool = await aiomysql.new_pool(password="zhangdapeng520")
    
    # 创建测试数据库
    await aiomysql.add_database(pool, test_db_name)
    pool.close()
    await pool.wait_closed()
    pool = await aiomysql.new_pool(db=test_db_name, password="zhangdapeng520")
    
    # 定义表结构
    columns = {
        "id": "INT PRIMARY KEY AUTO_INCREMENT",
        "name": "VARCHAR(50) NOT NULL",
        "created_at": "TIMESTAMP DEFAULT CURRENT_TIMESTAMP"
    }
    
    # 创建两个测试表
    await aiomysql.add_table(pool, test_table1, columns)
    await aiomysql.add_table(pool, test_table2, columns)
    
    # 获取所有表名
    all_tables = await aiomysql.get_all_table(pool)
    print(f"数据库 {test_db_name} 中的所有表:")
    for table in all_tables:
        print(f"- {table}")
    
    # 关闭连接池
    pool.close()
    await pool.wait_closed()
    
    # 清理测试数据库
    pool = await aiomysql.new_pool(password="zhangdapeng520")
    await aiomysql.delete_database(pool, test_db_name)
    pool.close()
    await pool.wait_closed()

if __name__ == "__main__":
    asyncio.run(main())